extends NodeState


@export var player: Player

var velocity: Vector2 = Vector2.ZERO


func _on_process(delta : float) -> void:
	player.position += velocity * delta



func _on_next_transitions() -> void:
	if player.current_health <= 0:
		transition_state(Player.STATE_DEATH)


func _on_enter() -> void:
	# 进入受伤状态后防止连续受击
	player.damageReceiver.set_deferred("monitorable", false)
	player.__protect_hurt_time = player.duration_protect_hurt

	process_animation()


func _on_exit() -> void:
	player.damageReceiver.set_deferred("monitorable", true)


func process_animation():
	player.animationPlayer.play(Player.ANIMATION_HURT)


# 受到攻击时的效果
func process_affected(damage_emitter: Area2D):
	if damage_emitter == null:
		return
	var direction: Vector2 = Vector2.RIGHT if damage_emitter.global_position.x < player.global_position.x else Vector2.LEFT
	velocity = direction * player.knockback_intensity


func on_hurt_animation_finished():
	transition_state(Player.STATE_IDLE)
